<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* I use the controller class to debug
 * Author Zijian Wang
 */

class Checkin extends CI_Controller {


	function insert()    // true/false       create_checkin
	{
		
		
		$u_name= get_cookie("username");
		if(empty($_POST['long'])||empty($_POST['lat'])){
			echo "lack geo-info!";
			return false;
		}
		$this->load->model(array('checkin_model','location_model'));
		
		$long = $_POST['long'];
		$lat  = $_POST['lat'];
		
		$result = $this->location_model->location_coordinate_near($lat,$long,10,0,1);
		if(count($result) == 0 ){
			$new_location = get_location_from_lat_long($lat,$long);
			$this->location_model->insert_location($lat, $long, $new_location['location']);
 		}
		$result = $this->location_model->location_coordinate_near($lat,$long,10,0,1);
 		$data = $this->checkin_model->create_checkin($result[0]['l_id'],$u_name);
		
		if($data){
			echo "You have checked in!!";
			$output  = 	$this->smarty->view('js/jump_to.tpl',array('msg'=>"CheckIn success","href"=>'/index.php'),true);
						$this->output->set_output($output);
		}
		return true;
	}

	function route()  // a list
	{
		$u_name= $_POST['u_name'];

		$this->load->model('checkin_model');
		$data = $this->checkin_model->checkin_route($u_name);
		
		var_dump($data);
	}
}

?>